package cn.enjoy.tool.juc;

import java.util.concurrent.CyclicBarrier;

/**
 * 让多个子线程一起计数和等待，等待的线程达到数值后，再统一唤醒
 * 基于ReentrantLock实现
 *
 * @author K
 * @date 2024-08-07 16:14
 */
public class T2CyclicBarrierTest {

    public static void main(String[] args) throws InterruptedException {
        /*
            tom到位，jack到位，rose到位
            导游发签证
            tom出发，jack出发，rose出发
        */

        CyclicBarrier barrier = new CyclicBarrier(3, () -> {
            System.out.println("等到各位大佬都到位之后，分发护照和签证等内容！");
        });

        new Thread(() -> {
            System.out.println("Tom到位！！！");
            try {
                barrier.await();
            } catch (Exception e) {
                System.out.println("悲剧，人没到齐！");
                return;
            }
            System.out.println("Tom出发！！！");
        }).start();
        Thread.sleep(100);
        new Thread(() -> {
            System.out.println("Jack到位！！！");
            try {
                barrier.await();
            } catch (Exception e) {
                System.out.println("悲剧，人没到齐！");
                return;
            }
            System.out.println("Jack出发！！！");
        }).start();
        Thread.sleep(100);
        new Thread(() -> {
            System.out.println("Rose到位！！！");
            try {
                barrier.await();
            } catch (Exception e) {
                System.out.println("悲剧，人没到齐！");
                return;
            }
            System.out.println("Rose出发！！！");
        }).start();

    }
}
